#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/.local/bin:/root/bin
. /etc/os-release

web_name=www.wp.com
web_conf=wp.conf
dir=wp
color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

config_nginx(){
if [[ $ID =~ centos|rocky ]];then
	[[ -f /etc/yum.repos.d/nginx.repo ]] ||  touch  /etc/yum.repos.d/nginx.repo
	cat >/etc/yum.repos.d/nginx.repo << EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

EOF
	yum -y install nginx &> /dev/null

	[[ -f /etc/nginx/conf.d/$web_conf ]] || touch  /etc/nginx/conf.d/$web_conf
	echo -e "\E[1;32m $web_conf  配置文件在 /etc/nginx/conf.d/$web_conf \E[0m"
	cat > /etc/nginx/conf.d/$web_conf << EOF


upstream webs {
server 172.16.1.7;
server 172.16.1.8;
}
server{
        listen 80;
        charset utf-8;
        server_name $web_name;
	root /data/nginx/$dir/;
        location / {
	proxy_pass http://webs;
	default_type text/html;
        root /data/nginx/$dir/;
        index index.html;
        }
	location ~* \.php$  {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php index.php;
                fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
                include        fastcgi_params;
	}
}
EOF
	[[ -d /data/nginx/$dir ]] || mkdir -p /data/nginx/$dir
	echo -e "\E[1;32m $web_conf 的家目录 在 /data/nginx/$dir   \E[0m"
	nginx -s reload
	chown -R nginx. /data/nginx/$dir		
fi
}

start_file(){
	systemctl start nginx	
	systemctl enable nginx
	systemctl restart nginx
	systemctl is-active nginx
	if [[ $? -eq 0 ]];then
		color "nginx服务配置成功" 0
	else 
		color "nginx服务配置失败" 1
		exit 1
	fi


}
config_nginx
start_file


